package com.zjw.controller

import com.zjw.common.{ResponseStatus, Result}
import com.zjw.service.UserService
import com.zjw.util.MD5Util
import org.apache.logging.log4j.scala.Logging
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation._

@RestController
class LoginController extends Logging {

  @Autowired
  var userService: UserService = _

  @CrossOrigin(Array("http://localhost:8000"))
  @RequestMapping(value = Array("/login"), method = Array(RequestMethod.POST))
  def login(@RequestBody user: Map[String, String]): Result = {
    logger.info("LoginController login function invoked.")
    val validatedUser = userService.getUserByUsernameAndPassword(
      user("username"),
      MD5Util.string2md5(user("password"))
    )
    if (null == validatedUser) {
      Result(ResponseStatus.FAILURE, null, "用户名密码错误, 未查询到结果.")
    } else {
      Result(ResponseStatus.SUCCESS, validatedUser)
    }
  }

  @RequestMapping(value = Array("/logout"), method = Array(RequestMethod.POST))
  def logout(): Person = {
    logger.info("HelloController hello function invoked.")
    new Person("zhangsan", 21)
  }

}
